package com.qfedu.dao.impl;

import com.qfedu.dao.AdministratorDao;
import com.qfedu.model.*;
import com.qfedu.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author han tao
 * @version 1.0
 * @date 2023/4/17 17:17
 * @project milky-tea
 */
public class AdministratorDaoImpl implements AdministratorDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());

    /**
     * 管理员登录
     * @param admin
     * @return
     */
    @Override
    public Administrator login(Administrator admin) {
        String sql = "select * from administrator where aname=? and password=?";
        List<Administrator> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Administrator.class), admin.getAname(), admin.getPassword());
        if (list != null && list.size() == 1){
            return list.get(0);
        }
        return null;
    }

    /**
     * 修改管理员信息
     * @param administrator
     * @return
     */
    @Override
    public int updateAdmin(Administrator administrator) {
        String sql = "update administrator set password=? where aname=?";
        int row = jdbcTemplate.update(sql,  administrator.getPassword(), administrator.getAname());
        return row;
    }

    /**
     * 添加奶茶
     * @param tea
     * @return
     */
    @Override
    public int addTea(MilkTea tea) {
        String sql = "insert into tea values(null,?,?,?,?)";
        int row = jdbcTemplate.update(sql, tea.getTname(), tea.getPrice(), tea.getGenre(), tea.getImg_path());
        return row;
    }

    /**
     * 修改奶茶数据
     * @param tea
     * @return
     */
    @Override
    public int updateTea(MilkTea tea) {
        String sql = "update tea set tname = ? , price = ? , genre = ? , img_path = ? where tid = ?";
        int row = jdbcTemplate.update(sql, tea.getTname(), tea.getPrice(), tea.getGenre(), tea.getImg_path(), tea.getTid());
        return row;
    }

    /**
     * 删除奶茶
     * @param tid
     * @return
     */
    @Override
    public int deleteTea(int tid) {
        String sql = "delete from tea where tid = ?";
        int row = jdbcTemplate.update(sql, tid);
        return row;
    }

    /**
     * 查询全部奶茶
     * @return
     */
    @Override
    public List<MilkTea> findAllTea() {
        String sql = "select * from tea";
        List<MilkTea> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MilkTea.class));
        return query;
    }

    /**
     * 根据ID查询奶茶
     * @param tid
     * @return
     */
    @Override
    public MilkTea findTeaByTid(int tid) {
        String sql="select * from tea where tid=?";
        List<MilkTea> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MilkTea.class), tid);
        if (list != null && list.size() == 1){
            return list.get(0);
        }
        return null;
    }

    /**
     * 查询用户
     * @return
     */
    @Override
    public List<User> findAllUser() {
        String sql = "select * from t_user";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        return list;
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @Override
    public int updateUserById(User user) {
        String sql = "update t_user set password=?,email=? where id=?";
        int update = jdbcTemplate.update(sql, user.getPassword(), user.getEmail(), user.getId());
        return update;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public int deleteById(int id) {
        String sql ="delete from t_user where id=?";
        int update = jdbcTemplate.update(sql, id);
        return update;

    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public User findUserById(int id) {
        String sql ="select * from t_user where id=?";
        List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), id);
        if (query != null && query.size() == 1){
            return query.get(0);
        }
        return null;
    }

    /**
     * 根据用户名查询
     * @param username
     * @return
     */
    @Override
    public User findUserByName(String username) {
        String sql ="select * from t_user where username=?";
        List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), username);
        if (query != null && query.size() == 1){
            return query.get(0);
        }
        return null;

    }
}
